package com.huangfu.learning.javabase.java.pattern.create.builder;


/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/8/7
 * Time: 20:04
 * Version: 1.0
 * Description: 另外一个例子
 **/
public class Build2 {
    public static void main(String[] args) {
        WolverineDirector director = new WolverineDirector();
        XMan wolverine = director.constructWolverine(new WolverineBuilder());
        String lover = wolverine.getLover();
        String getxFactor = wolverine.getxFactor();
        Integer age = wolverine.getAge();
        String name = wolverine.getName();
        System.out.println("Lover:"+lover+"  xFactor:"+getxFactor+" Name:"+name+" Age:"+age);
        //System.gc();
        System.out.println(wolverine.toString());
    }
}


interface XManBuilder {

    XManBuilder buildXFactor();

    XManBuilder buildLover();

    XManBuilder buildName();

    XManBuilder buildAge();

    XMan buildXman();
}

class XMan {

    private String name;

    private Integer age;

    private String xFactor;

    private String lover;

    public String getName() {
        return name;
    }

    public XMan setName(String name) {
        this.name = name;
        return this;
    }

    public Integer getAge() {
        return age;
    }

    public XMan setAge(Integer age) {
        this.age = age;
        return this;
    }

    public String getxFactor() {
        return xFactor;
    }

    public XMan setxFactor(String xFactor) {
        this.xFactor = xFactor;
        return this;
    }

    public String getLover() {
        return lover;
    }

    public XMan setLover(String lover) {
        this.lover = lover;
        return this;
    }
}

/**
 * 金刚狼的builder
 */
class WolverineBuilder implements XManBuilder {

    XMan mXMan;

    WolverineBuilder() {
        mXMan = new XMan();
    }


    @Override
    public XManBuilder buildXFactor() {
        mXMan.setxFactor("claw");
        System.out.println(mXMan.getxFactor());
        return this;
    }

    @Override
    public WolverineBuilder buildLover() {
        mXMan.setLover("Jane");
        System.out.println(mXMan.getLover());
        return this;
    }

    @Override
    public WolverineBuilder buildName() {
        mXMan.setName("Wali");
        System.out.println(mXMan.getName());
        return this;
    }

    @Override
    public WolverineBuilder buildAge() {
        mXMan.setAge(18);
        System.out.println(mXMan.getAge());
        return this;
    }

    @Override
    public XMan buildXman() {
        System.out.println("Wolverine is successfully built");
        return mXMan;
    }

}

/**
 *  控制builder生产金刚狼
 */
 class WolverineDirector {

    public XMan constructWolverine(WolverineBuilder wolverineBuilder) {
        return wolverineBuilder
                .buildXFactor()
                .buildLover()
                .buildName()
                .buildAge()
                .buildXman();
    }
}